<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Document</title>
	<link rel="stylesheet" href="./bootstrap/css/bootstrap.css">
	<link rel="stylesheet" href="./css/base.css">
	<link href="https://cdn.bootcdn.net/ajax/libs/lightbox2/2.9.0/css/lightbox.min.css" rel="stylesheet">
	<style>
		.crumbs {
			font-size: 14px;
			color: #ccc;
		}

		.content .item {
			margin-top: 10px;
			margin-bottom: 10px;
		}

		.content .item img {
			width: 100%;
			height: 100%;
			box-shadow: 0px 0px 10px #ccc;

		}

		.content .item .title:hover {
			text-decoration: underline;
			cursor: pointer;
		}


		.content .item div:first-child:hover img {
			opacity: 0.7;
			cursor: pointer;
			transition: all 0.3s ease-in-out;
		}

		.content .item .intro {
			margin-top: 10px;
			color: #ccc;
			font-size: 14px;
		}
	</style>
</head>

<body>
	<div class="container">
		<nav class="navbar navbar-default">
			<div class="container-fluid">
				<!-- Brand and toggle get grouped for better mobile display -->
				<div class="navbar-header">
					<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
						data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
						<span class="sr-only">Toggle navigation</span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
					<a class="navbar-brand" href="#" style="display:flex;">
						高清无码的博客
					</a>
				</div>

				<!-- Collect the nav links, forms, and other content for toggling -->
				<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

					<ul class="nav navbar-nav navbar-right">
						<li><a href="/">首页</a></li>
						<li class="dropdown">
							<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
								aria-haspopup="true" aria-expanded="false">分类 <span class="caret"></span></a>
							<ul class="dropdown-menu" id="cates">

							</ul>
						</li>
						<li><a href="/tags.html">标签</a></li>
						<li><a href="/about.html">关于我</a></li>
					</ul>
				</div><!-- /.navbar-collapse -->
			</div><!-- /.container-fluid -->
		</nav>

		<div>
			<h4 class="crumbs" style="text-align:left;">当前位置：首页 / 所有分类 / 体育 </h4>
		</div>

		<div class="content">
			<div class="artlist" id="categorical">
				

	
				</div>
			</div>
		</div>

		<button type="button" class="btn  btn-block" id="examine">查看更多</button>
		<hr>

		<div class="footer" style="text-align:center;font-size: 14px; color: #999;margin:10px 0;">
			首页 | 主站 | GitHub | created at 2020/02/4 | Powered by 高清无码
		</div>
	</div>
</body>
<script src="./js/jquery.js"></script>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.js"></script>
<script src="https://momentjs.bootcss.com/downloads/moment.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/lightbox2/2.9.0/js/lightbox-plus-jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.5.1/layer.js"></script>
<script>
	let page = 1;
	let pagesize = 2;
	let isFinish = false;
	let url = location.href;
	let reg = /\?cat_id=(.+)\&?/
	let [, cat_id] = Array.from(url.match(reg));
	if (isNaN(parseInt(cat_id))) {
		layer.msg(' 请求错误')
		location.href = '/'
	}
	initData()
	function loadCate() {
		return $.ajax({
			url: 'http://127.0.0.1:5686/api/cate'
		})
	}
	//加载更多
	$('#examine').on('click',function(res){
		
		if(isFinish){
			layer.msg('数据已经加载完毕',{icon:0})
			return;
		}
		$(this).prop('disabled',true).html('loading.....')
		page++;
		loadCateArticle(page,pagesize,cat_id).then(res =>{
			if(res.length < pagesize){
				isFinish = true;
			}
			$(this).prop('disabled',false).html('查看更多')
			let articlesHtml = renderCateArticle(res);
			let oldHtml = $('#categorical').html();
			console.log(oldHtml);
			console.log(articlesHtml);
			$('#categorical').html(oldHtml+articlesHtml)
		})
	})

	//加载分类文章
	function loadCateArticle(page, pagesize, cat_id) {
		NProgress.set(0.4)  // 开启默认0.4
		return $.ajax({
			url: `http://127.0.0.1:5686/api/categorical?cat_id=${cat_id}`,
			data: { page, pagesize },
			complete() {
				NProgress.done() //关闭
			}
		})
	}
	//渲染分类数据
	function renderCate(cates) {
		let lis = '';
		cates.forEach(cat => {
			let { id, name } = cat;
			// 拼接字符串li
			lis += `<li><a href="/cate.html?cat_id=${id}">${name}</a></li>`
		});
		return lis;
	}
	//渲染分类文章数据
	function renderCateArticle(article) {
		let divs = '';
		article.forEach(art => {
			let { id, title, cat_id, name, author, img, addtime } = art;


			if (!img) {
				img = 'https://img1.baidu.com/it/u=3049056019,2616222736&fm=26&fmt=auto&gp=0.jpg'
			} else {
				img = 'http://127.0.0.1:5686/' + img
			}
			addtime = moment(addtime).format("YYYY - MM - DD - HH:mm:ss")
			// 拼接字符串li
			divs += `<div class='item row'>
					<div class="col-md-2">
						
							<img src="${img}">
						
						
					</div>
					<div class="col-md-10">
						<div class="date ">作者：${author} 发布日期： ${addtime} 、分类：${name}</div>
						<a href='/detail.html?id=${id}'><div class="title">${title}</div></a>
					</div>
					<hr>
			</div>
			`
		})
		return divs;

	}
	async function initData() {
		NProgress.set(0.4) //默认0.4
		let pro1 = loadCateArticle(page, pagesize, cat_id);
		let pro2 = loadCate();
		let data = await Promise.all([pro1, pro2]);
		NProgress.done() // 关闭
		let [article, cates] = data;
		console.log(article, cates);
		localStorage.setItem('cates', JSON.stringify(cates));
		let lis = renderCate(cates);
		$('#cates').html(lis);
		let divs = renderCateArticle(article);
		$('#categorical').html(divs)
	}
</script>

</html>